<template>
  <div>
    <van-nav-bar title="注册页面">
      <template #left>
        <van-icon
          name="arrow-left"
          size=".6rem"
          @click="$router.back()"
          color="grey"
        />
      </template>
    </van-nav-bar>

    <div class="loginLogo">
      <img src="@/assets/icon/homeon.png" />
    </div>

    <van-form @submit="onSubmit">
      <van-field
        v-model="form.username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="form.password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <van-field
        v-model="form.password2"
        type="password"
        name="确认密码"
        label="确认密码"
        placeholder="确认密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        password2: "",
      },
    };
  },
  methods: {
    onSubmit() {
      console.log("注册", this.form);
      // 数据校验
      this.$api.fetchRegist(this.form).then((res) => {
        if (res.err == 1) {
          this.$toast({
            type: "fail",
            message: `${res.msg}`,
          });
          return;
        }
        this.$toast({
          type: "success",
          message: "登陆成功",
          onClose: () => {
            this.$router.replace("/login");
          },
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.loginLogo img {
  display: block;
  margin: 1.066667rem auto;
}
</style>